package com.oddfar.ztwz.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oddfar.ztwz.business.domain.entity.Friend;
import com.oddfar.ztwz.business.mapper.FridendMapper;
import com.oddfar.ztwz.business.service.FriendService;
import com.oddfar.ztwz.common.domain.R;
import com.oddfar.ztwz.common.domain.entity.SysUserEntity;
import com.oddfar.ztwz.framework.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FriendServiceImpl extends ServiceImpl<FridendMapper, Friend> implements FriendService {
    @Autowired
    private SysUserService sysUserService;
    @Override
    public R getFriendListByMyId(Long userId, String status) {
        try {
            // 检查传入的 userId 是否为空
            if (userId == null||status == null) {
                return R.ok(new ArrayList<>());
            }

            // 构建查询条件，筛选出与当前用户相关的好友关系
            LambdaQueryWrapper<Friend> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Friend::getStatus, status);

            long l = Long.parseLong(status);
            if (l == 0){
                queryWrapper.and(wrapper -> wrapper.eq(Friend::getFriendUserId, userId));
            }
            if (l == 1){
                queryWrapper.and(wrapper -> wrapper.eq(Friend::getUserId, userId).or().eq(Friend::getFriendUserId, userId));
            }

            // 查询好友关系列表
            List<Friend> friends = this.list(queryWrapper);

            if (friends != null && !friends.isEmpty()) {
                // 提取好友的用户 ID 列表
                List<Long> friendIds = friends.stream()
                        .map(friend -> friend.getUserId().equals(userId)? friend.getFriendUserId() : friend.getUserId())
                        .collect(Collectors.toList());

                // 根据好友 ID 列表查询好友用户信息
                List<SysUserEntity> sysUserEntities = sysUserService.listByIds(friendIds);
                return R.ok(sysUserEntities);
            }
            return R.ok(new ArrayList<>());
        } catch (Exception e) {
            // 记录异常信息，这里简单打印日志，实际项目中可使用日志框架
            e.printStackTrace();
            // 返回错误信息，可根据实际情况修改 R 类的错误处理逻辑
            return R.ok(new ArrayList<>());
        }
    }
    @Override
    public boolean removeFriend(Long userId, Long friendId) {
        if (userId == null || friendId == null) {
            return false;
        }
        return remove(new LambdaQueryWrapper<Friend>().eq(Friend::getUserId, userId)
                .eq(Friend::getFriendUserId, friendId)
                .or().eq(Friend::getUserId, friendId).eq(Friend::getFriendUserId, userId));
    }

    @Override
    @Transactional
    public boolean approveOrrejectFriend(Friend friend) {
        boolean update = update(friend, new LambdaQueryWrapper<Friend>()
                .eq(Friend::getUserId, friend.getUserId())
                .eq(Friend::getFriendUserId, friend.getFriendUserId())
                .or()
                .eq(Friend::getUserId, friend.getFriendUserId())
                .eq(Friend::getFriendUserId, friend.getUserId()));

        if(friend.getStatus()==2){
          boolean  b=remove(new LambdaQueryWrapper<Friend>().eq(Friend::getUserId, friend.getUserId()).eq(Friend::getFriendUserId, friend.getFriendUserId()));
            if(b&&update){
                return true;
            }
        }
        return update;
    }

    @Override
    public boolean existsFriendship(Long userId, Long friendUserId) {
            LambdaQueryWrapper<Friend> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.and(wrapper -> wrapper.eq(Friend::getUserId, userId).eq(Friend::getFriendUserId, friendUserId)
                    .or().eq(Friend::getUserId, friendUserId).eq(Friend::getFriendUserId, userId));
            return count(queryWrapper) > 0;
    }
}
